package com.gt.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.admin.domain.FlowConfig;
import com.gt.admin.domain.FlowDetail;
import com.gt.admin.domain.apiVO.FlowConfigVVO;
import com.gt.admin.domain.apiVO.Version;
import com.gt.admin.domain.vo.FlowConfigVO;
import com.gt.admin.mapper.FlowConfigMapper;
import com.gt.admin.mapper.FlowDetailMapper;
import com.gt.admin.service.IFlowConfigService;
import com.gt.admin.utils.Effect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 随访配置service
 */
@Service
public class FlowConfigServiceImpl extends ServiceImpl<FlowConfigMapper, FlowConfig> implements IFlowConfigService {
    @Autowired
    private FlowConfigMapper flowConfigMapper;
    @Autowired
    private FlowDetailMapper flowDetailMapper;

    /**
     * 查询版本信息
     */
    @Override
    public List<Version> selectVersion(Long superId) {
        return flowConfigMapper.selectVersion(superId);
    }

    /**
     * 按上级目录id查询随访配置列表
     */
    @Override
    public FlowConfigVVO selectBySuperId(Long superId) {
        Long[] ids = flowConfigMapper.selectIds(superId);
        FlowConfigVO flowConfigVO;
        List<FlowConfigVO> list = new ArrayList<>();
        FlowConfigVVO flowConfigVVO = new FlowConfigVVO();
        for (Long id : ids) {
            flowConfigVO = selectById(id);
            list.add(flowConfigVO);
            if (flowConfigVO.getEffect() == Effect.Effective){
                flowConfigVVO.setChooseVersion(flowConfigVO.getVersion());
            }
        }flowConfigVVO.setFlowConfigVOList(list);
        return flowConfigVVO;
    }

    /**
     * 根据id查询随访配置及随访明细
     */
    @Override
    public FlowConfigVO selectById(Long id) {
        FlowConfig flowConfig = getById(id);
        FlowConfigVO flowConfigVO = BeanUtil.copyProperties(flowConfig, FlowConfigVO.class);
        Long[] detIds = flowDetailMapper.selectDetIds(id);
        flowConfigVO.setFlowDetailList(flowDetailMapper.selectBatchIds(Arrays.asList(detIds)));
        return flowConfigVO;
    }

    /**
     * 新增或修改随访配置及明细，更新生效版本
     */
    @Transactional
    @Override
    public boolean update(FlowConfigVVO flowConfigVVO) {
        boolean flag = false;
        Long version = flowConfigVVO.getChooseVersion();
        List<FlowConfigVO> flowConfigVOList = flowConfigVVO.getFlowConfigVOList();
        Long superId = flowConfigVOList.get(1).getSuperId();
        LambdaUpdateWrapper<FlowConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(FlowConfig::getEffect, Effect.Invalid)
                .eq(FlowConfig::getSuperId, superId);
        update(updateWrapper);
        for (FlowConfigVO flowConfigVO : flowConfigVOList) {
            if (flowConfigVO.getVersion().longValue() == version.longValue()){
                flowConfigVO.setEffect(Effect.Effective);
                flag = saveOrUpdate(flowConfigVO);
                Long flowId = flowConfigVO.getFlowId();
                Long[] detIds = flowDetailMapper.selectDetIds(flowId);
                flowDetailMapper.deleteBatchIds(Arrays.asList(detIds));
                List<FlowDetail> flowDetailList = flowConfigVO.getFlowDetailList();
                for (FlowDetail flowDetail: flowDetailList) {
                    flowDetail.setFlowId(flowId);
                }
                if (flowConfigVOList.size()>0) {
                    flowDetailMapper.insertBatchSomeColumn(flowDetailList);

                }
            }
        }return flag;
    }

    /**+
     * 删除随访配置及明细
     */
    @Transactional
    @Override
    public boolean deleteById(Long id) {
        boolean flag = removeById(id);
        Long[] detIds = flowDetailMapper.selectDetIds(id);
        flowDetailMapper.deleteBatchIds(Arrays.asList(detIds));
        return false;
    }
}
